
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#include "cap.h"
#include "util.h"
#include "strategy.h"
#include "tf.h"

int main(int argc, char *argv[])
{
    // 全量配置寄存器应答报文：0F 03 30 00 00 1B 0B EF
    unsigned char tfConfig[] = {0x0F, 0x03, 0x36, 0x00, 0xEB, 0x00, 0xD5, 0x00, 0x05, 0x03,
                                0x84, 0x02, 0x58, 0x02, 0xA9, 0x01, 0x18, 0x00, 0x96, 0x01,
                                0x18, 0x00, 0x96, 0x00, 0x01, 0x02, 0x58, 0x00, 0x3C, 0x01,
                                0x3B, 0x00, 0x01, 0x00, 0x05, 0x03, 0xE8, 0x03, 0xE8, 0x03,
                                0xE8, 0x0E, 0x10, 0x00, 0x02, 0x00, 0x17, 0x00, 0x0A, 0x00,
                                0x12, 0x00, 0x09, 0x00, 0x28, 0x00, 0x20, 0x26, 0x91
                               };
    // 提取变压器CT、容量、档位数量
    process_config(tfConfig);

    // 从任一电容获取其控制参数：提取互感器变比信息
    char capCtrlData[] = {0x68, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x91, 0x21,
                          0x37, 0x34, 0x34, 0x35, 0x53, 0x33, 0xF6, 0x66, 0x76, 0x67,
                          0x6B, 0x66, 0x67, 0xE6, 0x66, 0x96, 0x66, 0x7B, 0x66, 0x96,
                          0x66, 0x66, 0x66, 0xC6, 0x6B, 0x6B, 0x66, 0x76, 0x96, 0x66,
                          0x6B, 0x76, 0x66, 0x0A, 0x16
                         };
    parse_ct(capCtrlData, &cap_sys);

    char zishu[] = {0x68, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x91, 0xB8,
                    0x32, 0x37, 0x33, 0x37, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                    0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x35, 0x33, 0x35, 0x33,
                    0x34, 0x33, 0x33, 0x34, 0xE3, 0x16
                   };
    // 轮询每一台的自描述信息：提取自身类型、额定容量
    {
        parse_cap_zishu_data(zishu);
    }

    // 策略初始化
    init_stategy();

    // 周期轮询电容器系统数据
    char sys[] = {0x68, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x91, 0x3A,
                  0x36, 0x34, 0x34, 0x35, 0x68, 0x75, 0x33, 0x33, 0x33, 0x33,
                  0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0x43, 0x58, 0x37, 0x33,
                  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x9B, 0x35,
                  0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                  0xA5, 0x3C, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xD8, 0x16
                 };
    parse_sys_data(sys, &cap_sys, &cap_up);

    unsigned char yyy[] = {0x0F, 0x02, 0x04, 0x41, 0x00, 0x00, 0x00, 0x00, 0x1e};
    T_YX yxs[32] = {0};
    process_yx(yyy, yxs);

    T_YC yc = {0};
    unsigned char ycc[] = {0x0F, 0x04, 0x24, 0x08, 0x32, 0x08, 0x27, 0x08, 0x31, 0x00,
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xB7, 0x13,
                           0x8A, 0x01, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0xC0,
                           0xF5
                          };
    process_yc(ycc, &yc);


    char capData[] = {0x68, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x91, 0x4F,
                      0x35, 0x34, 0x34, 0x35, 0x33, 0x33, 0x33, 0x33, 0x34, 0x33,
                      0x33, 0x35, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                      0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                      0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                      0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                      0x33, 0x33, 0x33, 0x33, 0x35, 0x33, 0x33, 0x33, 0x33, 0x33,
                      0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
                      0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x89,
                      0x16
                     };
    parse_cap_data(capData);
    /*
        T_CMD cmd = {0};
        while (true)
        {
            check_if_action(&rt, &cmd);
            if (cmd.length > 0)
            {
                // 调用485发送函数，把c->data发送出去
                // uart_send(cmd.data,cmd.length);
                free(cmd.data);
            }
            // 休眠几秒
        }
        */

    /*
        char a[] = {0x34, 0x82, 0x85, 0x81};
        double val = bcd_to_float(a, 4, 2, false);
        printf("val = %f  \r\n", val);
        double val2 = bcd_to_float(a, 4, 2, true);
        printf("val2 = %f", val2);
        printf("ct=%f  \r\n", cap_sys.ct);

        char cap[] = {0x33, 0x33, 0x33, 0x33, 0x35, 0x33, 0x33, 0x35, 0x33, 0x33, 0x33, 0x33, 0x33, 0x39, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x39, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x75, 0x34, 0x33, 0x33, 0x33, 0x33, 0x75, 0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x73, 0x35};
        char sys[] = {0xC8, 0x6A, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x9A, 0x33, 0x33, 0x33, 0x9C, 0x48, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x79, 0x74, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x86, 0x36, 0x33, 0x33, 0x33, 0x33, 0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0x36, 0x33, 0x33, 0x33, 0x33, 0x33};
        char zishu[] = {0x7A, 0x63, 0x64, 0x63, 0x63, 0x63, 0x64, 0x63, 0x63, 0x63, 0x63, 0x64, 0x65, 0x63, 0x65, 0x66, 0x63, 0x65, 0x65, 0x63, 0x63, 0x64, 0x66, 0x65, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x9A, 0xA2, 0xA1, 0x9A, 0x95, 0xA8, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xA6, 0x9B, 0x94, 0xA1, 0x97, 0xA2, 0xA1, 0x9A, 0x9E, 0x94, 0xA1, 0x9A, 0xA5, 0xA8, 0xA1, 0x97, 0x9C, 0x94, 0xA1, 0xA4, 0x9C, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x7E, 0x85, 0x60, 0x67, 0x68, 0x63, 0x8D, 0x86, 0x33, 0x33, 0x7B, 0x89, 0x63, 0x64, 0x63, 0x66, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x86, 0x89, 0x63, 0x64, 0x63, 0x63, 0x66, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x83, 0x37, 0x35, 0x33, 0x35, 0x33, 0x35, 0x33, 0x33, 0x34};
        char ctrl[] = {0x53, 0x33, 0xC3, 0x33, 0x43, 0x34, 0x38, 0x33, 0x34, 0xB3, 0x33, 0x63, 0x33, 0x48, 0x33, 0x63, 0x33, 0x33, 0x33, 0x93, 0x38, 0x38, 0x33, 0x43, 0x63, 0x33, 0x38, 0x43, 0x33};
        parse_cap_data(cap, caps);
        parse_cap_zishu_data(zishu, caps);
        parse_sys_data(1, sys, &cap_sys, &cap_up);
        parse_ct(ctrl, &cap_sys);

        unsigned char db[] = {0x88, 0xf4};
        printf("db=%f  \r\n", arr_to_double_big_endian(db, 2, 100, false));
        printf("db=%f  \r\n", arr_to_double_big_endian(db, 2, 100, true));

        char aaa = -55;
        printf("aaa = %d ,unsigned = %d\r\n", aaa, (unsigned char)aaa);

        unsigned char cc[] = {0x15, 0x02, 0x20, 0x00, 0x00, 0x20};
        unsigned short crc16 = CRC16_Calculate(cc, 6);
        printf("crc16= %d\r\n", crc16);

        unsigned char d[] = {0xf, 0xf, 0x10, 0x2, 0x0, 0x2, 0x1, 0x2};
        crc16 = CRC16(d, 8);
        printf("crc16= %d\r\n", crc16);

    */
    return 0;
}
